package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardReportMessage;
import com.jhgsys.internal.hazard.service.IHazardReportMessageService;
import com.jhgsys.internal.system.entity.User;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-12 09:55:48
 */
@Slf4j
@Validated
@Controller
public class HazardReportMessageController extends BaseController {

    @Autowired
    private IHazardReportMessageService hazardReportMessageService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportMessage")
    @RequiresPermissions("hazardReportMessage:view")
    public String hazardReportMessageIndex(){
        return FebsUtil.view("hazard/hazardReportMessage/hazardReportMessageList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportMessage/add")
    @RequiresPermissions("hazardReportMessage:add")
    public String hazardReportMessageAdd(){
        return FebsUtil.view("hazard/hazardReportMessage/hazardReportMessageEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportMessage/update/{id}")
    @RequiresPermissions("hazardReportMessage:edit")
    public String hazardReportMessageUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardReportMessage", hazardReportMessageService.findHazardReportMessageById(id));
        return FebsUtil.view("hazard/hazardReportMessage/hazardReportMessageEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     */
    @GetMapping("hazardReportMessage")
    @ResponseBody
    @RequiresPermissions("hazardReportMessage:view")
    public FebsResponse getAllHazardReportMessages(HazardReportMessage hazardReportMessage) {
        return new FebsResponse().success().data(hazardReportMessageService.findHazardReportMessageList(hazardReportMessage));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     */
    @GetMapping("hazardReportMessage/list")
    @ResponseBody
    @RequiresPermissions("hazardReportMessage:view")
    public FebsResponse hazardReportMessageList(QueryRequest request, HazardReportMessage hazardReportMessage) {
        Map<String, Object> dataTable = getDataTable(this.hazardReportMessageService.findHazardReportMessagePage(request, hazardReportMessage));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardReportMessage
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     */
    @Log("新增HazardReportMessage")
    @PostMapping("hazardReportMessage")
    @ResponseBody
    @RequiresPermissions("hazardReportPlan:reply")
    public FebsResponse addHazardReportMessage(@RequestBody HazardReportMessage hazardReportMessage) throws FebsException {
        try {
            Date date = new Date();
            User user = getCurrentUser();
            hazardReportMessage.setRealName(user.getUname());
            hazardReportMessage.setUserId(user.getUserId());
            hazardReportMessage.setUserNo(user.getUserNo());
            hazardReportMessage.setDeptId(user.getDeptId());
            hazardReportMessage.setDeptName(user.getDeptName());
            hazardReportMessage.setPostId(user.getPostId());
            hazardReportMessage.setPostName(user.getPostName());
            hazardReportMessage.setMessageDate(date);
            hazardReportMessage.setCreatedBy(user.getUserName());
            hazardReportMessage.setCreatedTime(date);
            hazardReportMessage.setDeletedFlg(FebsConstant.VALID);
            this.hazardReportMessageService.createHazardReportMessage(hazardReportMessage);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardReportMessage失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardReportMessage
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     */
    @Log("逻辑删除HazardReportMessage")
    @GetMapping("hazardReportMessage/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportMessage:delete")
    public FebsResponse deleteHazardReportMessage(@PathVariable("id") String hazardReportMessageIds) throws FebsException {
        try {
            this.hazardReportMessageService.deleteHazardReportMessage(hazardReportMessageIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportMessage失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardReportMessage
    * @author zhangwei
    * @date 2024-09-12 09:55:48
    */
    @Log("物理删除HazardReportMessage")
    @GetMapping("hazardReportMessage/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportMessage:delete")
    public FebsResponse physicallyDeleteHazardReportMessage(@PathVariable("id") String hazardReportMessageIds) throws FebsException {
        try {
            this.hazardReportMessageService.physicallyDeleteHazardReportMessage(hazardReportMessageIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportMessage失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardReportMessage
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     */
    @Log("修改HazardReportMessage")
    @PostMapping("hazardReportMessage/update")
    @ResponseBody
    @RequiresPermissions("hazardReportMessage:edit")
    public FebsResponse updateHazardReportMessage(@RequestBody HazardReportMessage hazardReportMessage) throws FebsException {
        try {
            this.hazardReportMessageService.updateHazardReportMessage(hazardReportMessage);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardReportMessage失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

}
